home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / UTILITY1 / MSWSRC35.ZIP / PRINT.CPP < prev    next >
C/C++ Source or Header  |  1992-11-24  |  5KB  |  252 lines

  1. /*
  2.  *      print.c         logo printing module                    dvb
  3.  *
  4.  *    Copyright (C) 1989 The Regents of the University of California
  5.  *    This Software may be copied and distributed for educational,
  6.  *    research, and not for profit purposes provided that this
  7.  *    copyright and statement are included in all such copies.
  8.  */
  9.  
  10. #include "logo.h"
  11. #include "globals.h"
  12. #include <stdarg.h>
  13. #include <conio.h>
  14.  
  15. int print_stringlen;
  16. char *print_stringptr;
  17. extern void real_print_help(), real_print_node();
  18. int x_margin=0, y_margin=0;
  19.  
  20. BOOLEAN print_backslashes = FALSE;
  21.  
  22. #ifdef mac
  23. BOOLEAN boldmode = 0;
  24. #endif
  25. #ifdef ibm
  26. extern BOOLEAN in_graphics_mode;
  27. #endif
  28.  
  29. void update_coords(char ch)
  30. {
  31. }
  32.  
  33. void print_char(FILE *strm, char ch)
  34. {
  35.   if (strm)
  36.       {
  37.       if (strm == stdout)
  38.         {
  39.             putcombochar(ch);
  40.         if (dribblestream != NULL) putc(ch, dribblestream);
  41.         update_coords(ch);
  42.     }
  43.      else
  44.        {
  45.       putc(ch, strm);
  46.  
  47.        }
  48.        }
  49.   else
  50.     {        /* printing to string */
  51.     if (--print_stringlen > 0) *print_stringptr++ = ch;
  52.     }
  53. }
  54.  
  55. void print_space(FILE *strm)
  56. {
  57.     print_char(strm,' ');
  58. }
  59.  
  60. /*VARARGS2*/
  61. void ndprintf(FILE *strm, char *fmt, ...)
  62. {
  63.     va_list ap;
  64.     NODE *nd;
  65.     char *cp;
  66.     char ch;
  67.  
  68.     va_start(ap,fmt);
  69.     while ((ch = *fmt++) != '\0') {
  70.     if (ch == '%') {
  71.         ch = *fmt++;
  72.         if (ch == 's')    /* show */
  73.         print_node(strm,va_arg(ap,NODE *));
  74.         else if (ch == 'p') { /* print */
  75.         nd = va_arg(ap,NODE *);
  76.         if (is_list(nd))
  77.             print_help(strm,nd);
  78.         else
  79.             print_node(strm,nd);
  80.         } else if (ch == 't') { /* text */
  81.         cp = va_arg(ap,char *);
  82.         while (ch = *cp++) print_char(strm,ch);
  83.         } else {
  84.         print_char(strm,'%');
  85.         print_char(strm,ch);
  86.         }
  87.     } else print_char(strm,ch);
  88.     }
  89.     va_end(ap);
  90. }
  91.  
  92. void real_print_help(FILE *strm, NODE *ndlist, int depth, int width)
  93. {
  94.     NODE *arg = NIL;
  95.     int wid = width;
  96.  
  97.     while (ndlist != NIL) {
  98.     arg = car(ndlist);
  99.     ndlist = cdr(ndlist);
  100.     if (check_throwing) break;
  101.     real_print_node(strm, arg, depth, width);
  102.     if (ndlist != NIL) {
  103.         print_space(strm);
  104.         if (--wid == 0) {
  105.         ndprintf(strm, "...");
  106.         break;
  107.         }
  108.     }
  109.     }
  110. }
  111.  
  112. void real_print_node(FILE *strm, NODE *nd, int depth, int width)
  113. {
  114.     int i;
  115.     char *cp;
  116.     NODETYPES ndty;
  117.  
  118.     if (depth == 0) {
  119.     ndprintf(strm, "...");
  120.     return;
  121.     }
  122.     if (nd == NIL) {
  123.     print_char(strm,'[');
  124.     print_char(strm,']');
  125.     } else if (nd == UNBOUND) {
  126.     ndprintf(strm, "UNBOUND");
  127.     } else if ((ndty = nodetype(nd)) & NT_PRIM) {
  128.     ndprintf(strm, "PRIM");
  129.     } else if (ndty & NT_LIST) {
  130.     print_char(strm,'[');
  131.     real_print_help(strm, nd, depth-1, width);
  132.     print_char(strm,']');
  133.     } else if (ndty == ARRAY) {
  134.     int i = 0, dim = getarrdim(nd), wid;
  135.     NODE **pp = getarrptr(nd);
  136.  
  137.     if (width < 0) wid = dim;
  138.     else wid = (dim > width ? width : dim);
  139.     print_char(strm,'{');
  140.     while (i < wid) {
  141.         real_print_node(strm,*pp++,depth-1,width);
  142.         if (++i < dim) print_space(strm);
  143.     }
  144.     if (wid < dim) ndprintf(strm, "...");
  145.     print_char(strm,'}');
  146.     if (print_backslashes && (getarrorg(nd) != 1)) {
  147.         char org[] = "@    ";
  148.  
  149.         sprintf(&org[1],"%d",getarrorg(nd));
  150.         ndprintf(strm,org);
  151.     }
  152.     } else if (ndty == QUOTE) {
  153.     print_char(strm, '\"');
  154.     print_node(strm, car(nd));
  155.     } else {
  156.     int wid;
  157.  
  158.     nd = cnv_node_to_strnode(nd);
  159.     cp = getstrptr(nd);
  160.     if (width < 0) wid = getstrlen(nd);
  161.     else {
  162.         wid = (width < 10 ? 10 : width);
  163.         wid = (wid < getstrlen(nd) ? wid : getstrlen(nd));
  164.     }
  165.  
  166.     if (print_backslashes == FALSE)
  167.         for (i = 0; i < wid; i++) {
  168.         print_char(strm,clearparity(*cp++));
  169.         }
  170.     else
  171.         for (i = 0; i < wid; i++) {
  172.         if (getparity(*cp)) {
  173.             print_char(strm,'\\');
  174.         }
  175.         print_char(strm,clearparity(*cp++));
  176.         }
  177.     if (wid < getstrlen(nd)) ndprintf(strm, "...");
  178.     gcref(nd);
  179.     }
  180. }
  181.  
  182. int find_limit(NODE *nd)
  183. {
  184.     int val = -1;
  185.  
  186.     if (nd == NIL) return(-1);
  187.     nd = cnv_node_to_numnode(valnode__caseobj(nd));
  188.     if (nodetype(nd) == INT) val = getint(nd);
  189.     gcref(nd);
  190.     return(val);
  191. }
  192.  
  193. void print_help(FILE *strm, NODE *nd)
  194. {
  195.     real_print_help(strm, nd, find_limit(Printdepthlimit),
  196.             find_limit(Printwidthlimit));
  197. }
  198.  
  199. void print_node(FILE *strm, NODE *nd)
  200. {
  201.     real_print_node(strm, nd, find_limit(Printdepthlimit),
  202.             find_limit(Printwidthlimit));
  203. }
  204.  
  205. void print_nobrak(FILE *strm, NODE *nd)
  206. {
  207.     if (is_list(nd)) print_help(strm, nd);
  208.     else print_node(strm, nd);
  209. }
  210.  
  211. void new_line(FILE *strm)
  212. {
  213.     print_char(strm,'\n');
  214. }
  215.  
  216. NODE *lshow(NODE *args)
  217. {
  218.     print_help(writestream, args);
  219.     new_line(writestream);
  220.     return(UNBOUND);
  221. }
  222.  
  223. void type_help(NODE *args, int sp)
  224. {
  225.     NODE *arg = NIL;
  226.  
  227.     while (args != NIL) {
  228.     arg = car(args);
  229.     args = cdr(args);
  230.     if (is_list(arg))
  231.         print_help(writestream, arg);
  232.     else
  233.         print_node(writestream, arg);
  234.     if (sp && (args != NIL)) {
  235.         print_space(writestream);
  236.     }
  237.     }
  238. }
  239.  
  240. NODE *ltype(NODE *args)
  241. {
  242.     type_help(args,0);
  243.     return(UNBOUND);
  244. }
  245.  
  246. NODE *lprint(NODE *args)
  247. {
  248.     type_help(args,1);
  249.     new_line(writestream);
  250.     return(UNBOUND);
  251. }
  252.